﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace SeCompUFSCarWeb.Models
{
    public class InscricaoMiniCurso
    {
        //Dados de Conexão
        string strconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        private int id;
        private int course;
        private string userId;
        private DateTime date;


        #region Getters and Setters
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }

        public string UserID
        {
            get { return userId; }
            set { userId = value; }
        }

        public int Course
        {
            get { return course; }
            set { course = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        #endregion

        public int VagasOcupadas()
        {
            int vagas = 0;

            //Utilizando a conexão
            MySqlConnection conn = new MySqlConnection(strconn);

            //INSERT Query
            string query = "SELECT COUNT(course_id) AS 'Vagas' FROM secomp.coursesignup WHERE course_id = @id";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            cmd.Parameters.AddWithValue("@id", course);


            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    vagas = reader.GetInt32("Vagas");
                }
            }
            catch (Exception)
            {
                return vagas;
            }
            finally
            {
                conn.Close();
            }

            return vagas;
        }

        public bool Inscrito()
        {
            string user = "";
            //Utilizando a conexão
            MySqlConnection conn = new MySqlConnection(strconn);

            //INSERT Query
            string query = "SELECT user_id FROM secomp.coursesignup WHERE user_id = @id";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            cmd.Parameters.AddWithValue("@id", userId);


            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    user = (string)reader["user_id"];
                }
            }
            catch (Exception)
            {
                return true;
            }
            finally
            {
                conn.Close();
            }

            if (user != "")
                return true;

            return false;
        }

        public bool Inscrever()
        {
            //Utilizando a conexão
            MySqlConnection conn = new MySqlConnection(strconn);

            string query = "INSERT INTO secomp.coursesignup (user_id, course_id, date) VALUES (@userID, @courseID, @date)";

            MySqlCommand cmd = new MySqlCommand(query, conn);
            cmd.Parameters.AddWithValue("@userID", userId);
            cmd.Parameters.AddWithValue("@courseID", course);
            cmd.Parameters.AddWithValue("@date", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                conn.Close();
            }

            return true;
        }


    }
}